package cn.year9;


import org.ho.yaml.YamlEncoder;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

public class TableShow {
    private String LINE = "=========================================";


    //show base table page
    public void showTable()
    {
        Message message = new Message();
        message.print(LINE,1);
        TdbHandle tdbHandleDao = new TdbHandle();
        TdbBase[] tdbBases =tdbHandleDao.openDatabase();
        message.print("index|\tTable |\tItemNum |\tCreateTime",1);
        for (int i=0;i<tdbBases.length;i++)
        {
            message.print("\t "+tdbBases[i].getIndex()+" \t "+tdbBases[i].getTable()+" \t "+tdbBases[i].getItemNum()+" \t "+tdbBases[i].getCreateTime(),1);
        }
        message.print(LINE,1);

        this.SelectCommand(); // 进行命令选择
    }

    //choose command
    public void SelectCommand()
    {
        Message message = new Message();
        Scanner scanner = new Scanner(System.in);
        TdbHandle tdbHandle = new TdbHandle();
        while (true) {
            message.send(MessageType.Info, "您可以选择 [0查询] [1删除] [2增加] [3重命名] [4退出程序] (输入序号): ");
            int commandindex = scanner.nextInt();
            File file = new File("Data.tdb");
            switch (commandindex) {
                case 0:
                    message.setPrefix("查询表");
                    message.send(MessageType.Info,"请输入需要查询的表Index:");
                    int index1 = scanner.nextInt();
                    if (index1 == 0)
                    {
                        message.send(MessageType.Warning,"无法操作基础表..\n");
                        showTable();
                        break;
                    }
                    TdbBase reslut = tdbHandle.searchTable(index1);
                    try {
                        message.setPrefix("查询 "+reslut.getItemNum() + " 条");
                        message.send(MessageType.Warning,"Table:" + reslut.getTable() +" "+reslut.getField()+"\n");
                        message.send(MessageType.Info, "您可以选择  [0删除] [1增加] [2更改] [3返回上级] (输入序号): ");

                        switch (scanner.nextInt())
                        {
                            case 0:
                                tdbHandle.removeTable(Integer.valueOf(reslut.getIndex())); //先删除旧表
                                message.send(MessageType.Info,"请输入要删除的字段Key:\n");
                                String key = scanner.next();

                                for (int i=0;i<Integer.valueOf(reslut.getItemNum());i++)
                                {
                                    if (reslut.getField().get(key) != null)
                                    {
                                        reslut.getField().remove(key);
                                        reslut.setItemNum(String.valueOf(Integer.valueOf(reslut.getItemNum()) - 1));
                                    }
                                }


                                YamlEncoder yamlEncoder = new YamlEncoder(new FileOutputStream(file,true));
                                yamlEncoder.writeObject(reslut);
                                yamlEncoder.flush();
                                yamlEncoder.close();
                                message.send(MessageType.Info,"删除成功! 还剩下" + reslut.getItemNum()+"条\n");
                                message.setPrefix("查询 "+reslut.getItemNum() + " 条");
                                message.send(MessageType.Warning,"Table:" + reslut.getTable() +" "+reslut.getField()+"\n");
                                showTable();
                                break;
                            case 1:

                                Map<String,String> reslutmap = reslut.field;
                                int acount=0;

                                while (true)
                                {
                                    tdbHandle.removeTable(Integer.valueOf(reslut.getIndex())); //先删除旧表
                                    message.send(MessageType.Info, "好的!接下来\n");
                                    message.send(MessageType.Info, "请键入字段名字 : ");
                                    String akey = scanner.next();
                                    message.send(MessageType.Info, "好的!接下来\n");
                                    message.send(MessageType.Info, "请键入字段内容 : ");
                                    String avalue = scanner.next();
                                    reslutmap.put(akey,avalue); //增加一个map
                                    acount++;
                                    message.send(MessageType.Info, "好的!请问还需要增加一个字段吗 [Y/增加] [N/不了] (回复Y/N):\n");
                                    if (scanner.next().equalsIgnoreCase("n"))
                                    {
                                        message.send(MessageType.Info, "好的!正在保存您的输入..\n");
                                        message.setPrefix(null);
                                        reslut.setItemNum(String.valueOf(Integer.valueOf(reslut.getItemNum()) + acount));
                                    }
                                    break;
                                }

                                YamlEncoder yamlEncoder1 = new YamlEncoder(new FileOutputStream(file,true));
                                yamlEncoder1.writeObject(reslut);
                                yamlEncoder1.flush();
                                yamlEncoder1.close();
                                message.send(MessageType.Info,"增加成功! 目前有" + reslut.getItemNum()+"条数据\n");
                                message.setPrefix("查询 "+reslut.getItemNum() + " 条");
                                message.send(MessageType.Warning,"Table:" + reslut.getTable() +" "+reslut.getField()+"\n");
                                showTable();
                            case 2:
                                tdbHandle.removeTable(Integer.valueOf(reslut.getIndex())); //先删除旧表
                                Map<String,String> retable = reslut.field;

                                message.send(MessageType.Info,"请输入要更改的字段Key:\n");
                                String retablekey = scanner.next();
                                message.print("已选中..",1);
                                message.send(MessageType.Info,"请输入字段的新内容:\n");
                                String retablevalue = scanner.next();
                                message.print("已获取..",1);
                                if (retable.get(retablekey) != null)
                                {
                                    retable.put(retablekey, retablevalue);
                                }
                                YamlEncoder yamlEncoder2 = new YamlEncoder(new FileOutputStream(file,true));
                                yamlEncoder2.writeObject(reslut);
                                yamlEncoder2.flush();
                                yamlEncoder2.close();
                                message.send(MessageType.Info,"更改成功! 目前有" + reslut.getItemNum()+"条数据\n");
                                message.setPrefix("查询 "+reslut.getItemNum() + " 条");
                                message.send(MessageType.Warning,"Table:" + reslut.getTable() +" "+reslut.getField()+"\n");
                                showTable();
                                break;
                            case 3:
                                break;
                                default:
                                    break;
                        }
                    }catch (Exception e)
                    {
                        message.send(MessageType.Warning,"该表或许不存在!\n");
                    }
                    break;
                case 1:
                    message.setPrefix("删除表");
                    message.send(MessageType.Info,"请输入需要删除的表Index:");
                    int index = scanner.nextInt();
                    if (tdbHandle.removeTable(index))
                    {
                        message.send(MessageType.Info,"成功将" + index + "删除!\n");
                    }else {
                        message.send(MessageType.Warning,"删除失败!\n");
                    }
                    showTable();
                    break;
                case 2:
                    message.setPrefix("新增");
                    int count=0;
                    Map<String,String> item = new HashMap<>();
                    message.send(MessageType.Info, "请输入表名:");
                    String tableName = scanner.next();
                    while (true)
                    {
                        message.send(MessageType.Info, "好的!接下来\n");
                        message.send(MessageType.Info, "请键入字段名字 : ");
                        String key = scanner.next();
                        message.send(MessageType.Info, "好的!接下来\n");
                        message.send(MessageType.Info, "请键入字段内容 : ");
                        String value = scanner.next();
                        item.put(key,value); //增加一个map
                        count++;
                        message.send(MessageType.Info, "好的!请问还需要增加一个字段吗 [Y/增加] [N/不了] (回复Y/N):\n");

                        if (scanner.next().equalsIgnoreCase("n"))
                        {
                            message.send(MessageType.Info, "好的!正在保存您的输入..\n");
                            message.setPrefix(null);
                            if (tdbHandle.createTable(tableName,String.valueOf(count),item))
                            {
                                message.send(MessageType.Info, "好的!已经为您保存数据表 "+tableName+" 及数据 "+count+" 条\n");
                                showTable();
                                break;
                            }
                        }
                    }

                case 3:
                    message.setPrefix("重命名表");
                    message.send(MessageType.Info,"请输入需要重命名的表Index:");
                    int index2 = scanner.nextInt();
                    message.send(MessageType.Info,"请输入为 "+index2+" 的重命名的新名称:");
                    String newName = scanner.next();
                    if (!tdbHandle.renameTable(index2,newName))
                    {
                        message.send(MessageType.Info,"修改失败!\n");
                        showTable();
                    }
                    message.send(MessageType.Info,"修改成功!\n");
                    showTable();
                    break;
                case 4:
                    message.send(MessageType.Info," 账户已注销!");
                    return;
                default:
                    message.send(MessageType.Warning, "您输入的命令有误!\n");
                    break;
            }
        }
    }



}
